package cn.itcast.tanhua.mytanhuaserver.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.itcast.tanhua.mytanhuadubbointerface.api.HuanXinApi;
import cn.itcast.tanhua.mytanhuaserver.service.BlacklistService;
import cn.itcast.tanhua.mytanhuaserver.service.MyCenterService;
import cn.itcast.tanhua.mytanhuaserver.service.UserInfoService;
import cn.itcast.tanhua.pojo.BlackList;
import cn.itcast.tanhua.pojo.User;
import cn.itcast.tanhua.pojo.UserInfo;
import cn.itcast.tanhua.utils.UserThreadLocal;
import cn.itcast.tanhua.vo.BlackListVo;
import cn.itcast.tanhua.vo.PageResult;
import cn.itcast.tanhua.vo.UserInfoVo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 郭文亮
 * @since 2021/12/4 9:58
 */
@Service
public class MyCenterServiceImpl implements MyCenterService {

    @Autowired
    private UserInfoService userInfoService;

    @Autowired
    private BlacklistService blacklistService;

    @Reference
    private HuanXinApi huanXinApi;

    @Override
    public UserInfoVo queryUserInfoByUserId(Long userId) {
        if (ObjectUtil.isEmpty(userId)) {
            //如果查询id为null，就表示查询当前用户信息
            userId = UserThreadLocal.get().getId();
        }
        //查询用户信息
        UserInfo userInfo = this.userInfoService.queryUserInfoByUserId(userId);
        if (ObjectUtil.isEmpty(userInfo)) {
            return null;
        }

        UserInfoVo userInfoVo = BeanUtil.copyProperties(userInfo, UserInfoVo.class, "marriage");
        userInfoVo.setGender(userInfo.getSex().toString().toLowerCase());
        userInfoVo.setMarriage(StrUtil.equals("已婚", userInfo.getMarriage()) ? 1 : 0);
        return userInfoVo;
    }

    /**
     * 查询黑名单用户列表
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageResult queryBlacklist(Integer page, Integer pageSize) {

        User user = UserThreadLocal.get();

        IPage<BlackList> blackListIPage = this.blacklistService.queryBlacklist(user.getId(), page, pageSize);

        PageResult pageResult = new PageResult();
        pageResult.setPage(page);
        pageResult.setPagesize(pageSize);
        pageResult.setCounts(Convert.toInt(blackListIPage.getTotal()));
        pageResult.setPages(Convert.toInt(blackListIPage.getPages()));

        List<BlackList> records = blackListIPage.getRecords();
        if(CollUtil.isEmpty(records)){
            return pageResult;
        }

        List<Object> userIds = CollUtil.getFieldValues(records, "blackUserId");
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoByUserIdList(userIds);

        List<BlackListVo> blackListVos = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            BlackListVo blackListVo = new BlackListVo();
            blackListVo.setAge(userInfo.getAge());
            blackListVo.setAvatar(userInfo.getLogo());
            blackListVo.setGender(userInfo.getSex().name().toLowerCase());
            blackListVo.setId(userInfo.getUserId());
            blackListVo.setNickname(userInfo.getNickName());

            blackListVos.add(blackListVo);
        }

        pageResult.setItems(blackListVos);

        return pageResult;

    }

    /**
     * 移除黑名单
     * @param userId
     */
    @Override
    public void delBlacklist(Long userId) {
        User user = UserThreadLocal.get();
        Boolean result = this.blacklistService.delBlacklist(user.getId(), userId);

        if (result){
            this.huanXinApi.deleteBlacklist(user.getId(), userId);

        }

    }
}
